<template>
  <div class="container">
      <el-card style="max-width: 480px; margin: auto; margin-top: 100px;">
          <el-form :model="registerForm" class="login-form" @submit.native.prevent="onSubmit" label-width="auto" style="max-width: 600px">
              <el-form-item label="用户名" :rules="[{ required: true, message: '请输入你的用户名', trigger: 'blur' }]">
                  <el-input v-model="registerForm.username" placeholder="Username"></el-input>
              </el-form-item>
              <el-form-item label="密码" :rules="[{ required: true, message: '请输入你的密码', trigger: 'blur' }]">
                  <el-input v-model="registerForm.password" type="password" placeholder="Password"></el-input>
              </el-form-item>
              <el-form-item label="邮箱" :rules="[{ required: true, message: '请输入你的邮箱', trigger: 'blur' }, { type: 'email', message: '请输入有效的邮箱地址', trigger: ['blur', 'change'] }]">
                <el-input v-model="registerForm.email" placeholder="Email"></el-input>
              </el-form-item>
              <el-form-item label="电话号码" :rules="[{ required: true, message: '请输入你的电话号码', trigger: 'blur' }, { pattern: /^[0-9]+$/, message: '请输入有效的电话号码', trigger: ['blur', 'change'] }]">
                <el-input v-model="registerForm.phoneNumber" placeholder="Phone Number"></el-input>
              </el-form-item>
              <el-form-item>
                  <el-button type="primary" @click="onSubmit" style="margin: 0 auto;">注册</el-button>
                  <el-button type="primary" @click="gotoLogin" style="margin: 0 auto;">返回</el-button>
              </el-form-item>
          </el-form>
      </el-card>
  </div>


</template>

<script setup>
import { reactive } from 'vue';
import { register } from "../../api/login.js"
import { setToken } from "../../utils/auth.js";
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()


const registerForm = reactive({
  username: '',
  phoneNumber: '',
  email: '',
  password: ''
});

const onSubmit = () => {
  register(registerForm).then(res => {
      ElMessage({
          message: "注册成功",
          type: 'success',
      })
  });
};

const gotoLogin = () => {
  router.push('/login');
}

</script>

<style scoped>
.login-form {
  width: 300px;
  margin: 100px auto;
}

.container {
  height: 100vh;
  width: 100vw;
}
</style>